package org.dizena.modules.wss.wsconfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

@Slf4j
@Component
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{

    @Resource
    private WstHandler handler;
    @Resource
    private WsInterceptor handShakeHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
    {

        log.info("wss connect {} ", registry.toString());

        String[] allowsOrigins = {"*"};

        registry.addHandler(handler, "/wss").setAllowedOrigins(allowsOrigins).addInterceptors(handShakeHandler);

        registry.addHandler(handler, "/wss/sockjs").setAllowedOrigins(allowsOrigins).addInterceptors(handShakeHandler).withSockJS();
    }

}